package com.etl.controller;

import com.etl.entity.EtlTaskMain;
import com.etl.enums.ResultEnum;
import com.etl.model.rev.EtlTaskListRev;
import com.etl.model.rev.TableStructureRev;
import com.etl.service.IEtlTaskMainService;
import com.etl.util.ResponseUtil;
import com.etl.util.ResultVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2019/10/21.
 */
@RestController
@RequestMapping("/etlTaskMain")
@Api(tags = {"全量任务管理"}, value = "任务配置相关接口")
public class EtlTaskMainController {

    @Autowired
    private IEtlTaskMainService etlTaskMainService;

    @ApiOperation(value = "获取某业务系统下的所有任务")
    @PutMapping(value = "/allTask")
    public ResultVO<PageInfo<EtlTaskMain>> allTask(@RequestBody EtlTaskListRev etlTaskListRev) {
        if (etlTaskListRev.getPages() == null || etlTaskListRev.getSizes() == null) {
            return ResponseUtil.error(ResultEnum.PARAMS_ERROR);
        }
        PageInfo<EtlTaskMain> pageInfo = etlTaskMainService.getAllTask(etlTaskListRev);
        return ResponseUtil.success(pageInfo);
    }

    @ApiOperation(value = "添加ETL任务")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "table", value = "表名", example = "cm_project", required = true),
            @ApiImplicitParam(paramType = "form", name = "sql1", value = "sql1", example = "查询语句", required = true),
            @ApiImplicitParam(paramType = "form", name = "batch", value = "系统名称", example = "1", required = true),
            @ApiImplicitParam(paramType = "form", name = "comment", value = "表的描述", example = "项目表", required = true),
            @ApiImplicitParam(paramType = "form", name = "tableType", value = "表类型(大小写)", example = "1", required = true)
    })
    @PostMapping(value = "/addTask", consumes = "application/x-www-form-urlencoded")
    public ResultVO addTask(String table, String sql1, Integer batch, String comment,Integer tableType) {
        if (Strings.isBlank(table) || Strings.isBlank(sql1) || Strings.isBlank(comment) || batch == null || tableType == null) {
            return ResponseUtil.error(ResultEnum.PARAMS_ERROR);
        }
        etlTaskMainService.addTask(table, sql1, batch, comment,tableType);
        return ResponseUtil.success();
    }


    @ApiOperation(value = "修改ETL任务")
    @PutMapping(value = "/updateTask")
    public ResultVO updateTask(@RequestBody EtlTaskMain etlTaskMain) {
        etlTaskMainService.updateTask(etlTaskMain);
        return ResponseUtil.success();
    }

    @ApiOperation(value = "执行单个ETL任务")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "kid", value = "任务主键", example = "13", required = true)
    })
    @GetMapping(value = "/executeSingle")
    public ResultVO executeSingle(Long kid) {
        etlTaskMainService.executeSingle(kid,"单点全量抽取作业","单点全量抽取作业");
        return ResponseUtil.success();
    }




    @ApiOperation(value = "删除任务与表结构")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "kid", value = "任务主键", example = "13", required = true),
            @ApiImplicitParam(paramType = "query", name = "type", value = "删除类型，type=1时 只删除任务，", example = "13", required = true),
    })
    @GetMapping(value = "/removeTaskAndTable")
    public ResultVO removeTaskAndTable(Long kid, Integer type) {
        etlTaskMainService.removeTaskAndTable(kid, type);
        return ResponseUtil.success();
    }



    @ApiOperation(value = "创建抽取目标表")
    @PutMapping(value = "/createTable")
    public ResultVO<Boolean> createTable(@RequestBody TableStructureRev tableStructure) {

        etlTaskMainService.createTable(tableStructure);

        return ResponseUtil.success();
    }

    @ApiOperation(value = "查询表是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "batch", value = "批次", example = "成本系统"),
            @ApiImplicitParam(paramType = "query", name = "tableName", value = "表名", example = "xxx")
    })
    @GetMapping(value = "/findTableByTableName")
    public ResultVO<Integer> findTableByTableName(Integer batch, String tableName) {
        if(Strings.isNotBlank(tableName)){
            tableName = tableName.toUpperCase();
        }
        Integer count = etlTaskMainService.findTableByTableName(batch, tableName);
        return ResponseUtil.success(count);
    }





    @ApiOperation(value = "通过表名或者sql查询数据， 返回10条数据，并将字段类型映射为 oracle 数据库的类型")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "tableName", value = "表名", example = "cm_project", required = true),
            @ApiImplicitParam(paramType = "form", name = "batch", value = "批次", example = "1", required = true)
    })
    @PostMapping(value = "/executeSql", consumes = "application/x-www-form-urlencoded")
    public ResultVO executeSql(String tableName, String batch) {
        if (Strings.isBlank(tableName) || Strings.isBlank(batch)) {
            return ResponseUtil.error(ResultEnum.PARAMS_ERROR);
        }
        return ResponseUtil.success(etlTaskMainService.executeSql(tableName, batch));
    }

}

